package com.ks.core.core;

import com.alibaba.fastjson.JSONObject;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsError;
import com.ks.core.exception.GunsException;
import com.ks.core.exception.GunsExceptionEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 统一API响应结果封装
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Result {
    private String version = "0.1";
    private int code;
    private String message;
    private Object data;

    public Result(BizExceptionEnum resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = null;
        this.version = "0.1";
    }

    public Result(BizExceptionEnum resultCode,BizExceptionEnum ErrorCode){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = new GunsError(ErrorCode);
        this.version = "0.1";
    }

    private Result setResult(String message) {
        this.code = 500;
        this.message = message;
        return this;
    }

    private Result setResult(GunsExceptionEnum resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        return this;
    }

    private Result setResult(BizExceptionEnum resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        return this;
    }

    private Result setResult(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        return this;
    }

    private Result setResult(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    private Result setResult(int code, String message,GunsError error) {
        this.code = code;
        this.message = message;
        this.data = error;
        return this;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    public static Result success() {
        return new Result().setResult(ResultCode.SUCCESS);
    }

    public static Result success(Object data) {
        return new Result().setResult(ResultCode.SUCCESS)
                .setData(data);
    }

    public static Result success(int result) {
        JSONObject json = new JSONObject();
        json.put("result", result);
        return new Result().setResult(ResultCode.SUCCESS)
                .setData(json);
    }

    public static Result fail() {
        return new Result().setResult(ResultCode.FAIL);
    }

    public static Result fail(BizExceptionEnum code) {
        return new Result().setResult(code);
    }

    public static Result fail(GunsExceptionEnum code) {
        return new Result().setResult(code);
    }

    public static Result fail(String message) {
        return new Result().setResult(message);
    }

    public static Result fail(GunsException e) {
        return new Result().setResult(e.getCode(), e.getMessage(),e.getData());
    }


}
